// components/huli/index.js
const app = getApp()
let {
  baseUrl,
  commonUploadDoctor,
  commonUpload,
  commonUpdate
} = app.globalData
Component({
  properties: {
    list: {
      type: Array
    },
    show: {
      type: Boolean
    },
    patient: {
      type: Boolean
    }
  },
  data: {
    commonUpload,
    commonUploadDoctor,
    commonUpdate,
    baseUrl,
    actions: [{
        name: '拍照',
      },
      {
        name: '从相册中选取照片',
      },

    ],
  },
  methods: {
    onClose() {
      this.setData({
        show: false
      })
    },
    onSelect(event) {
      console.log(event.detail);
      if (event.detail.name == '拍照') {
        let query = {
          count: 1,
          mediaType: ['image'],
          sourceType: ['camera'],
          camera: 'back',
        }
        this.photoCommon(query, '照片')
      } else {
        let query = {
          count: 1,
          mediaType: ['image'],
          sourceType: ['album'],
          sizeType: ['original'],
        }
        this.photoCommon(query, '照片')
      }
    },
    photoCommon(query, val) {
      let that = this
      let json = JSON
      wx.chooseMedia({
        ...query,
        success(res) {
          console.log('11111', res)
          that.setData({
            type: res.type
          })
          wx.showLoading({
            title: `正在上传${val}`,
            mask: true
          })
          //判断是医生还是患者
          let com = that.data.patient ? commonUpdate : commonUploadDoctor
          wx.uploadFile({
            url: baseUrl + com,
            filePath: res.tempFiles[0].tempFilePath,
            header: {
              Authorization: 'Bearer ' + wx.getStorageSync('token')
            },
            name: 'file',
            success(res) {
              let baseUrlLink = json.parse(res.data).data.path
              wx.showToast({
                title: "上传成功",
              })
              that.setData({
                show: false
              })
              that.triggerEvent('getIcon', {
                value: baseUrlLink
              })

            },
            fail(err) {
              console.log(err)
            }
          })
        },
        // fail(res) {
        //   console.log(res)
        //   console.log('图片拍摄失败')
        // }
      })
    },
    //文件上传
    afterRead() {
      let that = this
      console.log('222')
      wx.chooseMessageFile({
        count: 1,
        type: 'all',
        extension: ['.jpg', '.png', '.jpeg'],
        success: function (res) {
          // 文件选择成功
          var filePath = res.tempFiles[0].path;

          // 调用上传方法
          that.uploadFile(filePath);
        },
        fail: function (res) {
          // 文件选择失败
          console.log(res);
        }
      })
    },
    uploadFile(file, name) {
      console.log(file)
      let that = this
      let json = JSON
      wx.uploadFile({
        url: baseUrl + commonUploadDoctor,
        filePath: file,
        name: 'file',
        header: {
          Authorization: 'Bearer ' + wx.getStorageSync('token')
        },
        success(res) {
          console.log('success', res)
          /*    res.name = json.parse(res.data).data.path.match(/\.(\w+)$/)
             // 展示文件数据
             that.data.fileList.push({
               url: baseUrl + commonUpload + json.parse(res.data).data.path,
               name: res.name[1]
             })
             that.data.fileListCopy.push(
               json.parse(res.data).data.path
             ) */
          that.setData({
            'ops.icon': json.parse(res.data).data.path,
          })
          wx.showToast({
            title: '上传文件成功',
          })
        }
      })
    },
  }

})